package com.prp.gwt.client.widgets.user.article;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.prp.gwt.client.ClientConstants;
import com.prp.gwt.client.user.article.ArticleHistoryHandler;
import com.prp.gwt.client.user.article.ArticleListener;
import com.prp.gwt.client.user.php.PHPHistoryHandler;
import com.prp.gwt.client.user.profile.ProfileListener;
import com.prp.gwt.client.vo.SubjectRecommend;

public class RecommendSummaryItem extends VerticalPanel {

	public RecommendSummaryItem(final SubjectRecommend recommend) {
		HorizontalPanel innerPanel = new HorizontalPanel();
		addStyleName("recommend-summary");
		{
			VerticalPanel vPanel = new VerticalPanel();
			Image img = new Image(recommend.getUser().getPortrait());
			img.addStyleName("recommend-summary-image");
			vPanel.add(img);
			HTML author = new HTML("<a>" + recommend.getUser().getUsername()
					+ "</a>");
			author.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					History.newItem(PHPHistoryHandler.PREFIX_USERHOME
							+ recommend.getUser().getId());
				}
			});
			author.addStyleName("recommend-summary-label");
			vPanel.add(author);
			innerPanel.add(vPanel);
		}
		VerticalPanel vPanel = new VerticalPanel();
		vPanel.addStyleName("recommend-summary-content");
		HorizontalPanel titlePanel = new HorizontalPanel();
		HTML title = new HTML("<a>" + recommend.getTitle() + "</a>");
		title.addStyleName("recommend-summary-title");
		titlePanel.add(title);
		vPanel.add(titlePanel);
		HorizontalPanel panel = new HorizontalPanel();
		vPanel.add(panel);
		HTML content = new HTML(recommend.getContent());
		content.addStyleName("recommend-summary-text");
		vPanel.add(content);
		panel = new HorizontalPanel();
		Label label = new Label("(" + recommend.getReplyNum() + "/"
				+ recommend.getClickFreq() + ")");
		panel.add(label);
		vPanel.add(panel);
		{
			HorizontalPanel scorePanel = new HorizontalPanel();
			// scorePanel.addStyleName("recommend-summary-score");
			label = new Label("评分:");
			label.addStyleName("score-text");
			scorePanel.add(label);
			Label score = new Label(recommend.getScore() + "分");
			score.addStyleName("star" + recommend.getScore().intValue());
			scorePanel.add(score);
			score.setTitle(recommend.getScore() + "分");
			vPanel.add(scorePanel);
		}
		innerPanel.add(vPanel);
		add(innerPanel);
		HTML del = new HTML("<a>删除</a>");
		if (ProfileListener.getInstance().user != null
				&& ProfileListener.getInstance().user.getIdentity() >= ClientConstants.ADMIN)
			innerPanel.add(del);
		del.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				ArticleListener.getInstance().deleteRecommendation(
						recommend.getId(), RecommendSummaryItem.this);
			}
		});
		title.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				History.newItem(ArticleHistoryHandler.PREFIX_RECOMMEND
						+ recommend.getId());
			}
		});

	}

}
